-- The addon namespace.
local addon, ns = ...

-- Define the shield charge icon class and put it in the addon namespace.
local ShieldChargeIcon = {}
local ShieldChargeIconMetatable = { __index = ShieldChargeIcon }
ns.ShieldChargeIcon = ShieldChargeIcon

-- Inherit methods from the Icon class.
setmetatable(ShieldChargeIcon, { __index = ns.Icon })

-- Constructor.
function ShieldChargeIcon:new(name)
	local self = ns.Icon:new(name)
	setmetatable(self, ShieldChargeIconMetatable)
	
	-- Set the Shield Charge texture and spell name.
	local spellId = 156321
	local spellName, _, texture = GetSpellInfo(spellId)
    self.Texture:SetTexture(texture)
	self.SpellName = spellName
	self.SpellId = spellId
	
	-- Place the duration text in the center of the icon.
	self.DurationText:ClearAllPoints()
	self.DurationText:SetPoint("CENTER")
	
	-- Dragging.
	self.Icon:SetScript("OnDragStop", self.OnDragStopHandler)
	
	return self
end

-- Handler that stops dragging and saves the position to the saved variables.
-- Note that self is the icon frame and not the icon class.
function ShieldChargeIcon:OnDragStopHandler()
	self:StopMovingOrSizing()
	ShieldMaidConfig.shieldChargeAnchor, _, _, ShieldMaidConfig.shieldChargeX, ShieldMaidConfig.shieldChargeY = self:GetPoint(1)
end

-- (Re)loads the icon with values from the saved variables config.
function ShieldChargeIcon:Reload()
	self:ReloadBase()
	
	-- Set the position.
	self.Icon:SetPoint(ShieldMaidConfig.shieldChargeAnchor, ShieldMaidConfig.shieldChargeX, ShieldMaidConfig.shieldChargeY)
end

-- Updates the icon.
function ShieldChargeIcon:Update(currentRage)
	local name, _, _, _, _, _, expires = UnitBuff("player", self.SpellName)
	local systemTime = GetTime()
	
	if name then
		self:UpdateTint(currentRage, 0)
		self.DurationText:SetText(self:RoundNumber(expires - systemTime))
		self.Icon:SetAlpha(ns.Config.activeAlpha)
	else
		self.Icon:SetAlpha(ns.Config.inactiveAlpha)
		self:UpdateTint(currentRage, 20)
		self.DurationText:SetText("")     
	end
	
	self:UpdateCooldown()
end	